/**
   UserMessgaeController
 * Time:2018年6月6日
 */
package com.ssm.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.entity.Page;
import com.ssm.entity.PageCount;
import com.ssm.entity.Usermessage;
import com.ssm.service.UserMessageService;

/**
 * @author 15199
 *
 */
@RequestMapping("/UserMessage")
@Controller
@SuppressWarnings("all")
public class UserMessageController {

	@Resource UserMessageService userMessageService;
	private int pageMax;
	private int pageSize=5;
	@RequestMapping("/list.do")
	public ModelAndView list(){
		int count=userMessageService.getCount();
		/**计算分页所需的数据 **/
		PageCount pageCount=new PageCount(count,pageSize);
		pageMax=pageCount.getPageMax();
		List<Usermessage> list=userMessageService.getAll(new Page(0,pageSize),new Usermessage());
		ModelAndView mav=new ModelAndView("/AE/ShowUserMsg");
		mav.addObject("list",list);
		mav.addObject("pageMax",pageMax);  
		mav.addObject("pageNum",1);
		return mav;
	}
	
	@RequestMapping("/data.do")
	public ModelAndView data(
			String userid,
			String userstate
			,HttpServletRequest request
			,HttpServletResponse response){
		String pageNum=request.getParameter("pageNum");
		Usermessage usermessage=new Usermessage();
		if(userid!=null){
			usermessage.setUserid(Integer.parseInt(userid));
		}
		if(userstate!=null){
			usermessage.setUserstate(Integer.parseInt(userstate));
		}
		List<Usermessage> list=userMessageService.getAll(new Page((Integer.parseInt(pageNum)-1)*pageSize,pageSize),new Usermessage());
		ModelAndView mav=new ModelAndView("/AE/ShowUserMsg");
		mav.addObject("list",list);
		mav.addObject("pageMax",pageMax);  
		mav.addObject("pageNum",Integer.parseInt(pageNum));
		return mav;
	}
	@RequestMapping("/updateState")
	public void updateState(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String id=request.getParameter("id");
		Usermessage usermessage=new Usermessage();
		usermessage.setUserid(Integer.parseInt(id));
		userMessageService.updateStats(usermessage);
		response.sendRedirect("/UserMessage/list.do");

	}







}
